<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
            <a-row :gutter="24">
                <a-col :span="4">
                    <a-form-item label="应用" name="appCode">
                        <a-input v-model:value="searchFormState.appCode" placeholder="appCode" />
                    </a-form-item>
                </a-col>
                <a-col :span="4">
                    <a-form-item label="版本" name="appVersion">
                        <a-input v-model:value="searchFormState.appVersion" placeholder="请输入" />
                    </a-form-item>
                </a-col>
                <a-col :span="4">
                    <a-form-item label="哪端" name="propertyType">
                        <a-select v-model:value="searchFormState.propertyType" placeholder="请选择" :options="option1"
                            allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="4">
                    <a-form-item label="类型" name="grayscaleType">
                        <a-select v-model:value="searchFormState.grayscaleType" placeholder="请选择" :options="option2"
                            allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="4">
                    <a-form-item label="账号" name="grayscaleAccounts">
                        <a-input v-model:value="searchFormState.grayscaleAccounts" placeholder="请输入" />
                    </a-form-item>
                </a-col>
                <a-col :span="4">
                    <a-button type="primary" @click="table.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
                </a-col>
            </a-row>
        </a-form>
        <s-table ref="table" :columns="columns" :data="loadData" :alert="options.alert.show" bordered
            :row-key="(record) => record.grayscaleId" :tool-config="toolConfig" :row-selection="options.rowSelection">
            <template #operator class="table-operator">
                <a-space>
                    <a-upload :accept="fileType" :customRequest="importRoster" :showUploadList="false">
                        <a-button :loading="uploading" type="primary">导入</a-button>
                    </a-upload>
                    <a-button danger @click="deleteRess()" :loading="uploading">删除</a-button>
                </a-space>
            </template>
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)">编辑</a>
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteRe(record)">
                            <a-button type="link" danger size="small">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="greylisting">
import { message } from 'ant-design-vue'
import Form from './form.vue'
import grayscaleApi from '@/api/biz/grayscaleApi'
const fileType = '.xlsx,.xls'
let searchFormState = reactive({})
const searchFormRef = ref()
const table = ref()
const formRef = ref()
const uploading = ref(false)
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
const columns = [
    
    {
        title: '姓名',
        dataIndex: 'grayscaleName'
    },
    {
        title: '账号',
        dataIndex: 'grayscaleAccounts'
    },
    {
        title: '应用名称',
        dataIndex: 'appName'
    },
    {
        title: '应用ID',
        dataIndex: 'appCode'
    },
    {
        title: '应用版本',
        dataIndex: 'appVersion'
    },
    {
        title: '操作',
        dataIndex: 'action',
        align: 'center',
        width: '150px'
    }

]
const option1 = [
    { value: 'pc', label: 'PC' },
    { value: 'html5', label: 'Html5' },
    { value: 'android', label: 'Android' },
    { value: 'ios', label: 'IOS' },
    { value: 'umap', label: 'UMAP' },
]
const option2 = [
    { value: '0', label: '灰度名单' },
    { value: '1', label: '黑名单' },
    { value: '2', label: '白名单' }
]
const importRoster = async (file) => {
    let arr= ['grayscaleType', 'propertyType', 'appCode']
    if (searchFormState.propertyType != 'pc') {
        arr.push('appVersion')
    }
    // const arr = ['grayscaleType', 'propertyType', 'appCode', 'appVersion']
    const bool = arr.some(v => !searchFormState[v])
    if (bool) {
        message.warning('请填写完整数据')
        return false
    }
    uploading.value = true
    const form = new FormData()
    form.append('file', file.file)
    form.append('grayscaleType', searchFormState.grayscaleType)
    form.append('propertyType', searchFormState.propertyType)
    form.append('appCode', searchFormState.appCode)
    if (searchFormState.propertyType != 'pc') {
        form.append('appVersion', searchFormState.appVersion)
    }
    const uuid = await grayscaleApi.cimport(form)
    const impotSta = await grayscaleApi.getcimport({ uuid })
    // 只有导入中是true
    if (impotSta) {
        const timer = setInterval(() => {
            grayscaleApi.getcimport({ uuid }).then(r => {
                if (!r) {
                    clearInterval(timer)
                    table.value.refresh(true)
                }
            })
        }, 1000);
    } else {
        table.value.refresh(true)
    }
    uploading.value = false
}
let selectedRowKeys = ref([])
// 列表选择配置
const options = {
    alert: {
        show: false,
        clear: () => {
            selectedRowKeys = ref([])
        }
    },
    rowSelection: {
        onChange: (selectedRowKey, selectedRows) => {
            selectedRowKeys.value = selectedRowKey
        }
    }
}
const loadData = (parameter) => {
    const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
    return grayscaleApi.getcpage(Object.assign(parameter, searchFormParam)).then((data) => {
        // if (data?.records?.length > 0) {
        //     data.records.forEach((item, index) => {
        //         item.num = index + 1
        //     })
        // }
        return data
    }).catch(() => {
        return []
    })
}
// 删除
const deleteRe = (record) => {
    let params = [
        {
            grayscaleId: record.grayscaleId
        }
    ]
    grayscaleApi.cdelete(params).then(() => {
        table.value.refresh(true)
    })
}
// 批量删除
const deleteRess = () => {
    if (selectedRowKeys.value.length < 1) {
        message.warning('请选择一条或多条数据')
        return false
    }
    const params = selectedRowKeys.value.map((m) => {
        return {
            grayscaleId: m
        }
    })
    grayscaleApi.cdelete(params).then(() => {
        table.value.clearRefreshSelected()
    })
}
</script>
